/*
 * Copyright (C) 2017-2025
 * ZKMall All rights reserved, Designed By 深圳中科鑫智科技有限公司
 * Copyright authorization contact 18814114118
 */
package com.shop.zkmall.admin.service.message.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.shop.zkmall.admin.dao.message.CerePlatfromMessageDAO;
import com.shop.zkmall.admin.param.message.MessageDeleteParam;
import com.shop.zkmall.admin.param.message.MessageGetAllParam;
import com.shop.zkmall.admin.param.message.MessageSaveParam;
import com.shop.zkmall.admin.param.message.MessageUpdateParam;
import com.shop.zkmall.admin.service.log.CerePlatformLogService;
import com.shop.zkmall.admin.service.message.CerePlatfromMessageService;
import com.shop.zkmall.commons.domain.common.Page;
import com.shop.zkmall.commons.domain.message.CerePlatfromMessage;
import com.shop.zkmall.commons.domain.user.CerePlatformUser;
import com.shop.zkmall.commons.exception.CoBusinessException;
import com.shop.zkmall.commons.utils.TimeUtils;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author zkmall zkmallvip@163.com
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class CerePlatfromMessageServiceImpl implements CerePlatfromMessageService {

    private final CerePlatfromMessageDAO cerePlatfromMessageDAO;

    private final CerePlatformLogService cerePlatformLogService;

    @Override
    @Transactional(isolation = Isolation.DEFAULT, propagation = Propagation.REQUIRED, rollbackFor = {CoBusinessException.class, Exception.class})
    public void save(MessageSaveParam param, CerePlatformUser user) {
        String time = TimeUtils.yyMMddHHmmss();
        CerePlatfromMessage message = new CerePlatfromMessage();
        //新增短信模板
        message.setCreateTime(time);
        message.setMessageProject(param.getMessageProject());
        message.setMessageTemplate(param.getMessageTemplate());
        message.setTemplateCode(param.getTemplateCode());
        message.setTemplateDescribe(param.getTemplateDescribe());
        message.setTemplateSign(param.getTemplateSign());
        cerePlatfromMessageDAO.insert(message);
        //新增日志
        cerePlatformLogService.addLog(user, "短信配置", "平台端操作", "添加短信模板", String.valueOf(message.getMessageId()), time);
    }

    @Override
    @Transactional(isolation = Isolation.DEFAULT, propagation = Propagation.REQUIRED, rollbackFor = {CoBusinessException.class, Exception.class})
    public void update(MessageUpdateParam param, CerePlatformUser user) {
        String time = TimeUtils.yyMMddHHmmss();
        CerePlatfromMessage message = new CerePlatfromMessage();
        //修改短信模板
        message.setUpdateTime(time);
        message.setMessageProject(param.getMessageProject());
        message.setMessageTemplate(param.getMessageTemplate());
        message.setTemplateCode(param.getTemplateCode());
        message.setTemplateDescribe(param.getTemplateDescribe());
        message.setTemplateSign(param.getTemplateSign());
        cerePlatfromMessageDAO.updateByPrimaryKeySelective(message);
        //新增日志
        cerePlatformLogService.addLog(user, "短信配置", "平台端操作", "修改短信模板", String.valueOf(message.getMessageId()), time);
    }

    @Override
    @Transactional(isolation = Isolation.DEFAULT, propagation = Propagation.REQUIRED, rollbackFor = {CoBusinessException.class, Exception.class})
    public void delete(MessageDeleteParam param, CerePlatformUser user) {
        String time = TimeUtils.yyMMddHHmmss();
        //删除短信模板
        cerePlatfromMessageDAO.deleteByPrimaryKey(param.getMessageId());
        //新增日志
        cerePlatformLogService.addLog(user, "短信配置", "平台端操作", "删除短信模板", String.valueOf(param.getMessageId()), time);
    }

    @Override
    public CerePlatfromMessage getById(Long messageId) {
        return cerePlatfromMessageDAO.getById(messageId);
    }

    @Override
    public Page<CerePlatfromMessage> getAll(MessageGetAllParam param) {
        PageHelper.startPage(param.getPage(), param.getPageSize());
        List<CerePlatfromMessage> list = cerePlatfromMessageDAO.getAll(param);
        PageInfo<CerePlatfromMessage> pageInfo = new PageInfo<>(list);
        return new Page<>(pageInfo.getList(), pageInfo.getTotal());
    }
}
